<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\upload;

use app\controller\property\Base;
use api\upload\exception\UploadException;
use app\common\repositories\system\upload\SystemUploadFileRepository;


class UploadFile extends Base
{

    /**
     * 获取上传的图片列表
     * @return void
     */
    public function uploadFileList()
    {
        $where = $this->request->param([
            'group_id' => -1
        ]);
        $where['file_type'] = ['image/png', 'image/jpeg', 'image/gif', 'image/jpg'];
        /**  @var SystemUploadFileRepository $SystemUploadFileRepository */
        $SystemUploadFileRepository = app()->make(SystemUploadFileRepository::class);
        [$page, $limit] = $this->getPage(1, 15);
        $res = $SystemUploadFileRepository->getList($this->request->propertyId, $where, 'id,show_src file_path,file_name name', $page, $limit);
        return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
    }

    /**
     * 删除上传的文件
     *
     * @return void
     */
    public function uploadFiledDel()
    {
        $id = (array)$this->request->param('id');
        foreach ($id as $k => $v) {
            $v = intval($v);
            if ($v <= 0) {
                unset($id[$k]);
                continue;
            }
            $id[$k] = $v;
        }

        if (empty($id)) {
            return json()->data(['code' => -1, 'msg' => '参数错误']);
        }
        /**  @var SystemUploadFileRepository $SystemUploadFileRepository */
        $SystemUploadFileRepository = app()->make(SystemUploadFileRepository::class);

        $list = $SystemUploadFileRepository->selectWhere([
            ['id', 'in', $id],
            ['property_id', '=', $this->request->propertyId]
        ]);

        $num = 0;
        $successIds = [];
        foreach ($list as $k => $v) {
            $res = $SystemUploadFileRepository->delUploadFile($v);
            if ($res) {
                $num++;
                $successIds[] = $v['id'];
            }
        }
        if ($num > 0) {
            return json()->data(['code' => 0, 'msg' => '成功删除' . $num . '个文件', 'data' => ['id' => $successIds]]);
        } else {
            return json()->data(['code' => -1, 'msg' => '删除失败']);
        }
    }

    /**
     * 移动上传上传的文件组
     *
     * @return void
     */
    public function uploadFileMove()
    {
        $id = (array)$this->request->param('id');
        foreach ($id as $k => $v) {
            $v = intval($v);
            if ($v <= 0) {
                unset($id[$k]);
                continue;
            }
            $id[$k] = $v;
        }
        $groupId = $this->request->param('group_id', 0, 'intval');
        $groupId = $groupId <= 0 ? 0 : $groupId;

        if (empty($id)) {
            return json()->data(['code' => -1, 'msg' => '参数错误']);
        }
        /**
         * @var SystemUploadFileRepository $SystemUploadFileRepository
         */
        $SystemUploadFileRepository = app()->make(SystemUploadFileRepository::class);
        $res = $SystemUploadFileRepository->whereUpdate([
            ['id', 'in', $id]
        ], [
            'group_id' => $groupId
        ]);

        if ($res) {
            return json()->data(['code' => 0, 'msg' => '移动成功']);
        } else {
            return json()->data(['code' => -1, 'msg' => '移动失败']);
        }
    }

    /**
     * 上传图片
     *
     * @return \think\response\Json
     */
    public function uploadImageAdd()
    {
        $field = ($this->request->param('field') ? $this->request->param('field') : 'file');
        $dir = ($this->request->param('dir') ? $this->request->param('dir') : 'home');
        try {
            $upload = \app\common\services\UploadService::init($this->request->propertyId, $dir);
            $res = $upload->uploadImageFile($field);
            $fileInfo = $res['data']['fileInfo'];
            $groupId = $this->request->param('group_id', 0, 'intval');
            $data = [
                'source' => 'property',
                'group_id' => $groupId <= 0 ? 0 : $groupId,
                'show_src' => $fileInfo['show_src'],
                'upload_path' => $fileInfo['upload_path'],
                'storage_type' => $fileInfo['storage_type'],
                'file_md5' => $fileInfo['file_md5'],
                'file_size' => $fileInfo['file_size'],
                'file_type' => $fileInfo['file_type'],
                'file_name' => $fileInfo['file_name'],
                'user_id' => $this->request->propertyUserInfo['user_id'],
                'property_id' => $this->request->propertyId
            ];
            /**
             * @var SystemUploadFileRepository $SystemUploadFileRepository
             */
            $SystemUploadFileRepository = app()->make(SystemUploadFileRepository::class);
            $SystemUploadFileRepository->create($data);
            return json()->data(['code' => 0, 'msg' => '上传成功', 'data' => ['src' => $res['data']['src']]
            ]);
        } catch (UploadException $e) {
            exception_log('上传文件错误', $e);
            return json()->data(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
}